import Cesium from '../Ces/Cesium'
import Util from '../Core/Util'
import WeUniform from '../Display/WeUniform'

class FluidUniform {
  weUniform: WeUniform
  constructor(terrainImage) {
    const { west, south, east, north } = terrainImage
    const rectangle = Cesium.Rectangle.fromDegrees(west, south, east, north)
    const position = new Cesium.Cartesian3()
    Cesium.Cartesian3.fromRadians(
      rectangle.west + rectangle.width * 0.5,
      rectangle.south + rectangle.height * 0.5,
      0.0, Cesium.Ellipsoid.default, position)

    const vec3Uniform = {
      rtcCenter: position
    }

    const meter = Util.computeRectangleMeter(rectangle)

    const vec4Uniform = {
      extent: rectangle
    }

    const { width, height } = terrainImage.source
    const floatUniform = {
      numLon: width ?? 256,
      numLat: height ?? 256,
      shouldFlipY: 1.0,
      extrusionScale: 1.0,
      merterWidth: meter.width,
      merterHeight: meter.height,
    }

    this.weUniform = new WeUniform({
      floatUniform,
      vec3Uniform,
      vec4Uniform,
      name: 'FluidMesh_Uniform'
    })
  }

  get rtcCenter() {
    return this.weUniform.Properties.rtcCenter
  }

  get width() {
    return this.weUniform.Properties.numLon
  }

  get height() {
    return this.weUniform.Properties.numLat
  }

  setupUniformMap(uniformMap) {
    this.weUniform.setupUniformMap(uniformMap)
  }

  getSource() {
    return this.weUniform.getSource(true)
  }

  destroy() {
    return Cesium.destroyObject(this)
  }
  isDestroyed() {
    return false
  }
}

export default FluidUniform
